#include "MysqlConnector.h"

#include "MysqlSession.h"

#include <algorithm>
#include <iostream>
#include <memory>

MysqlConnector::MysqlConnector(const std::string &conn_str, int pool_max_size)
{
    client_ = std::make_unique<mysqlx::Client>(conn_str, mysqlx::ClientOption::POOL_MAX_SIZE, pool_max_size);
}

MysqlConnector::~MysqlConnector()
{
    client_->close();
}

std::shared_ptr<MysqlSession> MysqlConnector::getSession()
{
    try {
        mysqlx::Session sess      = client_->getSession();
        MysqlSession   *mysqlSess = new MysqlSession(std::move(sess));
        return std::shared_ptr<MysqlSession>(mysqlSess);
    } catch (const std::exception &e) {
        std::cerr << "getSession failed: " << e.what() << std::endl;
    }
    return std::shared_ptr<MysqlSession>();
}
